package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.dao.AddressDao;
import com.qf.dao.CartDao;
import com.qf.dao.GoodsDao;
import com.qf.dao.OrderDao;
import com.qf.pojo.*;
import com.qf.service.AddressService;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    AddressService addressService;
    @Autowired
    CartDao cartDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    AddressDao addressDao;
    @Autowired
    GoodsDao goodsDao;

    @RequestMapping("/getOrderView")
    public String getOrderView(HttpServletRequest req) {
        //获取用户购物车信息
        User user = (User) req.getSession().getAttribute("user");
        List<Order> carts = orderService.getOrderList(user.getId());
        //获取用户地址信息
        List<Address> addressList = addressService.getAddressList(user.getId());
        //存储到请求域
        req.setAttribute("carts", carts);
        req.setAttribute("addList", addressList);

        //响应前端
        return "forward:/order";
    }

    @RequestMapping("/addOrder")
    public String addOrder(HttpServletRequest req) {
        //获取收货地址
        int aid = Integer.parseInt(req.getParameter("aid"));
        //获取用户信息
        User user = (User) req.getSession().getAttribute("user");

        Order order = orderService.addOrder(aid, user.getId());

        //清空购物车
        cartDao.clearCart(user.getId());

        //存储到请求域
        req.setAttribute("order", order);
        //响应前端
        return "forward:/orderSuccess";

    }

    @RequestMapping("/getOrderList")
    public String getOrderList(HttpServletRequest req) {
        //获取用户信息
        User user = (User) req.getSession().getAttribute("user");

        List<Order> orderList = orderService.getOrderList(user.getId());

        //发送信息到请求域
        req.setAttribute("orderList", orderList);

        //请求转发
        return "forward:/orderList";

    }

    @RequestMapping("/getOrderDetail")
    public String getOrderDetail(HttpServletRequest req){

        String oid = req.getParameter("oid");

        OrderDetail orderDetail = orderService.getOrderDetail(oid);

        Order order = orderDao.getOrder(orderDetail.getOid());
        orderDetail.setOrder(order);

        Address addressList = addressDao.getAddress(order.getAid());
        orderDetail.setAddress(addressList);

        Goods goods = goodsDao.findById(orderDetail.getPid());
        orderDetail.setGoods(goods);

        req.setAttribute("od", orderDetail);

        return "forward:/orderDetail";
    }

    @RequestMapping("/paySuccess")
    public String paySuccess(HttpServletRequest req) {
        String result = req.getParameter("result");

        JSONObject jsonObject = JSON.parseObject(result);
        Object result1 = jsonObject.get("result");
        JSONObject jsonObject1 = JSON.parseObject(result1.toString());
        String return_code = jsonObject1.getString("return_code");
        String orderId = jsonObject1.getString("out_trade_no");
        //如果支付成功了, 更新订单的状态
        if ("SUCCESS".equalsIgnoreCase(return_code)) {
            orderService.updateOrderStatus(orderId, "2");
        }

        return "redirect:/order?method=getOrderList";
    }

}
